package com.honey.feature;

import java.io.File;

import org.java.plugin.Plugin;

import com.honey.core.Extension;
import com.honey.core.compiler.Compilation;
import com.honey.core.compiler.CompilationType;
import com.honey.core.feature.Feature;
import com.honey.core.feature.SourceCodeFeature;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.utils.Constant;
import com.honey.feature.java.JavaFeature;
import com.honey.feature.properties.PropertiesFeature;
import com.honey.feature.sql.SqlFeature;
import com.honey.feature.text.TextFeature;
import com.honey.feature.xml.XmlFeature;
import com.honey.standard.writer.io.IoUtility;

public class FeatureExtension extends Extension<Plugin> implements SourceCodeFeature{
	
	private static final int BLOCK_BODY_IDENTITY =1000;
	
	private static final String BLOCK_BODY_NAME ="B";

	private static final String FEATURE_PLUGIN_NAME="HoneyFileFeature";
	
	private static final CompilationType[] COMPILATION_TYPE = new CompilationType[]{
			CompilationType.JAVA,
			CompilationType.XML,
			CompilationType.TXT,
			CompilationType.CSS,
			CompilationType.HTML,
			CompilationType.JS,
			CompilationType.JSP,
			CompilationType.PROPERTIES,
			CompilationType.SQL,
	};
	
	private BlockFeature blockFeature ;
	
	public FeatureExtension(Plugin belongPlugin) {
		super(belongPlugin);
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.feature.SourceCodeFeature#calculationFeature(com.honey.core.compiler.Compilation, com.honey.core.compiler.CompilationType)
	 */
	@Override
	public boolean calculationFeature(Compilation compilation,CompilationType compilationType,String charset ) {
		CalculationFeature calculationFeature = getCalculationFeature(compilationType);
		String feature = null;
		boolean answer = false;
		try{
			feature = calculationFeature.getFeature(compilation,charset);
			if( feature !=  null ){
				blockFeature = new BlockFeature(BLOCK_BODY_IDENTITY,BLOCK_BODY_NAME, feature);
				compilation.addAfterBodyComment(
						new StringBuilder()
						.append(Constant.PROJECT_NAME)
						.append(" - OriginalChecksum:")
						.append(BLOCK_BODY_NAME)
						.append(feature).append(" (do not edit this line)")
						.toString()
				);
				answer = true ;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return answer;
	}
	
	@Override
	public boolean originalChecksum(String workspace, FullyQualifiedJavaType type, CompilationType compilationType,Feature[] features,String charset) {
		boolean answer = false ;
		CalculationFeature calculationFeature = getCalculationFeature(compilationType);
		File compilationFile =  IoUtility.referenceFile(type, compilationType);
		String p =compilationFile.getParent();
		File targetDirectory = p == null ? new File(workspace):new File(workspace,p) ;
		File target =  new File(targetDirectory,compilationFile.getName() );
		if( ! target.exists() ){
			answer = true ;
		}else{
			answer = calculationFeature.originalChecksum(target, features,charset);
		}
		return answer;
	}

	private CalculationFeature getCalculationFeature(CompilationType compilationType){
		CalculationFeature answer = null;
		switch( compilationType ){
			case JAVA : //java类抽取
				answer = new JavaFeature();
				break;
				
			case HTML:
			case XML:
				answer = new XmlFeature();
				break;
			case PROPERTIES:
				answer = new PropertiesFeature();
				break;
			case SQL:
				answer = new SqlFeature();
				break;	
				
			default: //其它文本文件抽取
				answer = new TextFeature();
		}
		return answer; 
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.feature.SourceCodeFeature#getCalculaterName()
	 */
	@Override
	public String getCalculaterName() {
		return FEATURE_PLUGIN_NAME;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.feature.SourceCodeFeature#getFeatures()
	 */
	@Override
	public Feature[] getFeatures() {
		return new Feature[]{
				blockFeature,
		};
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.feature.SourceCodeFeature#supportCompilationTypes()
	 */
	@Override
	public CompilationType[] supportCompilationTypes() {
		return COMPILATION_TYPE;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.Extension#clean()
	 */
	@Override
	public void clean() {
	}
}
